﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Models;
using EnterpriseFund.Common;
using System.Data;
using DevExpress.Web.Mvc;
using System.Configuration;

namespace EnterpriseFund.Controllers
{


    [OutputCache(Duration = 0, VaryByParam = "none")]   
    public class IndexController : AdminBaseController
    {

        public ActionResult Index()
        {
            BLL.PrvUser prvUserBLL = new BLL.PrvUser();
            string entityNo = ConfigurationManager.AppSettings["EntityNo"].ToString();
            string loginid = OperateSession.get("LogID");
            string currenttheme = prvUserBLL.GetThemePrvUser(entityNo, loginid);
            if (string.IsNullOrEmpty(currenttheme))
            {
                bool bl = prvUserBLL.UpdateThemePrvUser(entityNo, loginid, OperateSession.usertheme);
                OperateSession.SetToCacheRequest(OperateSession.usertheme, "CurrentUserTheme");
            }
            else
            {
                OperateSession.SetToCacheRequest(currenttheme, "CurrentUserTheme");
            }
           
            ViewData["DeptName"] = "";
            ViewData["EmpName"] = "";
            string loginName = OperateSession.get(GlobalConstant.PARAM_USERNAME);
            //获取部门信息
            string emplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            //string entityNo = GlobalConstant.ENTITYNO;
            if (emplNo != "" && entityNo != "")
            {
                BLL.HREmpl hREmplBLL = new BLL.HREmpl();
                DataSet ds = hREmplBLL.GetEmplFromViewByEmplNo(emplNo, entityNo);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataTable dt = ds.Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["FIsMainPosition"].ToString() == "Y")
                        {
                            ViewData["EmpName"] = dt.Rows[i]["FEmpName"].ToString();
                            ViewData["DeptName"] = dt.Rows[i]["FDeptName"].ToString();
                            OperateSession.add(GlobalConstant.PARAM_DEPTNO, dt.Rows[i]["FDeptNo"].ToString());
                            break;
                        }

                    }
                }
            }
            BLL.Common commonBLL = new BLL.Common();
            DataSet yds = commonBLL.GetYear();
            List<int> yearList = new List<int>();
            for (int i = 0; i < yds.Tables[0].Rows.Count; i++)
            {
                yearList.Add(int.Parse(yds.Tables[0].Rows[i]["FYear"].ToString()));
            }

            ViewData["YearData"] = yearList;
            if (OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR) != null && OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR) != "")
            {

            }
            else
            {
                
                OperateSession.add(GlobalConstant.PARAM_SYSYEAR, yearList.Max().ToString());
            }


            ViewData["selYear"] = OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR);
            ViewData["tmetable"] = themetable;
            // string[] sts = DevExpress.Web.ASPxClasses.Internal.ThemedProducts.MVCExtensions.GetControlElements("MVCxMenu");
         
            ViewData["themepic"] = getbackgroundurl().ToString();
            ViewData["Color"] = getbackgroundcolor().ToString();

            ViewData["Forecolor"] = getbackgroundforecolor().ToString();
            
            return View();
        }
        public string SetTheme(string tem)
        {


            // string temout = OperateSession.ls[tem].ToString();
            string temout = themetable.Select("ThemeName='" + tem + "'")[0]["ThemeName"].ToString();
            if (!string.IsNullOrEmpty(temout))
            {

                BLL.PrvUser prvUserBLL = new BLL.PrvUser();
                OperateSession.SetToCacheRequest(temout, "CurrentUserTheme");
                string entityNo = ConfigurationManager.AppSettings["EntityNo"].ToString();
                string loginid = OperateSession.get("LogID");
                bool bl = prvUserBLL.UpdateThemePrvUser(entityNo, loginid, temout);
                // OperateSession.usertheme = tem;
                return bl ? "ok" : "false";
            }
            else
            {
                return "false";
            }


        }
        private DataTable _themetable;
        public DataTable themetable
        {
            set
            {

            }
            get
            {
                if (_themetable == null)
                {
                    _themetable = new DataTable();
                    _themetable.Columns.Add("ThemeName");
                    _themetable.Columns.Add("Picture");
                    _themetable.Columns.Add("Color");
                    _themetable.Columns.Add("CnName");
                    _themetable.Columns.Add("ForeColor");
                    _themetable.TableName = "ThemeClass";
                    _themetable.ReadXml(Server.MapPath("~") + "\\ThemeClass.xml");
                    return _themetable;
                }
                else
                {
                    return _themetable;
                }
            }
        }
        public string getbackgroundurl()
        {
            if (string.IsNullOrEmpty(OperateSession.GetFromCacheRequest("CurrentUserTheme").ToString()))
            {
                OperateSession.SetToCacheRequest(OperateSession.usertheme, "CurrentUserTheme");

                if (EnterpriseFund.Common.OperateSession.GetFromCacheRequest("CurrentUserTheme").ToString() == "Aqua")
                {
                    return @Url.Content("~/Content/images/header01.jpg", true);
                }
                else
                {
                    DataRow[] dr = themetable.Select("ThemeName='" + EnterpriseFund.Common.OperateSession.GetFromCacheRequest("CurrentUserTheme").ToString() + "'");
                    return DevExpress.Web.ASPxClasses.Internal.ResourceManager.GetResourceUrl(null, Url.Content("~/App_Themes" + dr[0]["Picture"].ToString()));
                }
            }
            else
            {
                if (EnterpriseFund.Common.OperateSession.GetFromCacheRequest("CurrentUserTheme").ToString() == "Aqua")
                {
                    return @Url.Content("~/Content/images/header01.jpg", true);
                }
                else
                {
                    DataRow[] dr = themetable.Select("ThemeName='" + EnterpriseFund.Common.OperateSession.GetFromCacheRequest("CurrentUserTheme").ToString() + "'");
                    return DevExpress.Web.ASPxClasses.Internal.ResourceManager.GetResourceUrl(null, Url.Content("~/App_Themes" + dr[0]["Picture"].ToString()));
                }
            }
        }
        public string getbackgroundcolor()
        {
            if (string.IsNullOrEmpty(OperateSession.GetFromCacheRequest("CurrentUserTheme").ToString()))
            {
                OperateSession.SetToCacheRequest(OperateSession.usertheme, "CurrentUserTheme");


                DataRow[] dr = themetable.Select("ThemeName='" + EnterpriseFund.Common.OperateSession.GetFromCacheRequest("CurrentUserTheme").ToString() + "'");
                return dr[0]["Color"].ToString();

            }
            else
            {

                DataRow[] dr = themetable.Select("ThemeName='" + EnterpriseFund.Common.OperateSession.GetFromCacheRequest("CurrentUserTheme").ToString() + "'");
                return dr[0]["Color"].ToString();

            }
        }
        public string getbackgroundforecolor()
        {
            if (string.IsNullOrEmpty(OperateSession.GetFromCacheRequest("CurrentUserTheme").ToString()))
            {
                OperateSession.SetToCacheRequest(OperateSession.usertheme, "CurrentUserTheme");


                DataRow[] dr = themetable.Select("ThemeName='" + EnterpriseFund.Common.OperateSession.GetFromCacheRequest("CurrentUserTheme").ToString() + "'");
                return dr[0]["ForeColor"].ToString();

            }
            else
            {

                DataRow[] dr = themetable.Select("ThemeName='" + EnterpriseFund.Common.OperateSession.GetFromCacheRequest("CurrentUserTheme").ToString() + "'");
                return dr[0]["ForeColor"].ToString();

            }
        }
        public string getbackgroundurlwiththemestring(string str)
        {
            DataRow[] dr = themetable.Select("ThemeName='" + str + "'");
            return DevExpress.Web.ASPxClasses.Internal.ResourceManager.GetResourceUrl(null, Url.Content("~/App_Themes" + dr[0]["Picture"].ToString()));

        }
        public ActionResult Top()
        {
            List<string > yearList=new List<string>();
            yearList.Add("2015");
            yearList.Add("2014");
            yearList.Add("2013");
            yearList.Add("2012");
            yearList.Add("2011");
            ViewData["YearData"] = yearList;
            if (OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR) != null && OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR) != "")
            {
             
            }
            else
            {
                OperateSession.add(GlobalConstant.PARAM_SYSYEAR, "2014");
            }
            ViewData["selYear"] = OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR);
            return View();
        }

        public static string WorkPrvFunctList(string tablename,string sq)
        {
            switch (tablename)
            {
                case "t_AllowanceIssue":
                    switch (sq)
                    {
                        case "30":
                            return "m_AllowanceIssueCheck";
                    }
                    break;
            }
            return "";
        }

        

        /// <summary>
        /// 获取提醒信息
        /// </summary>
        /// <returns></returns>
        public string GetRemindListMsg()
        {
            string empid = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);//税务机关代码
            BLL.WorkFlowToDo w = new BLL.WorkFlowToDo();
            BLL.PrvFunctList prvFunctListBLL = new BLL.PrvFunctList();
            DataSet ds = prvFunctListBLL.getFunctList(OperateSession.get(GlobalConstant.PARAM_LOGINID), GlobalConstant.ENTITYNO);
            List<Model.WorkFlowToDo> wl = w.GetModelList("FStatus='M'");
            int count = 0;
            foreach (Model.WorkFlowToDo wft in wl)
            {
                string pname = WorkPrvFunctList(wft.FTableName, wft.FSeq.ToString());
                if (pname != "")
                {
                    DataRow[] drmain = ds.Tables[0].Select("FFunctNo='" + pname + "'");
                    if (drmain.Count() > 0)
                    {
                        count++;
                    }

                }
            }
            string msg = "";
            if (count > 0)
            {
                msg = "<a  href='#' onclick='Dlg()'>" + "你有" + count + "条留言信息" + "</a>";
                return "1#" + msg;
            }
            else
            {
                return "";
            }
        }

        public ActionResult Buttom()
        {

            ViewData["DeptName"] = "";
            ViewData["EmpName"] = "";
            string loginName = OperateSession.get(GlobalConstant.PARAM_USERNAME);
            //获取部门信息
            string emplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            string entityNo = GlobalConstant.ENTITYNO;
            if (emplNo != "" && entityNo != "")
            {
                BLL.HREmpl hREmplBLL = new BLL.HREmpl();
                DataSet ds = hREmplBLL.GetEmplFromViewByEmplNo(emplNo, entityNo);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataTable dt = ds.Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["FIsMainPosition"].ToString() == "Y")
                        {
                            ViewData["EmpName"] = dt.Rows[i]["FEmpName"].ToString();
                            ViewData["DeptName"] = dt.Rows[i]["FDeptName"].ToString();
                            OperateSession.add(GlobalConstant.PARAM_DEPTNO, dt.Rows[i]["FDeptNo"].ToString());
                            break;
                        }

                    }
                }
            }
            return View();
        }
        /// <summary>
        /// 注销
        /// </summary>
        public void Logout()
        {

            OperateSession.del(GlobalConstant.PARAM_USERNAME);
            OperateSession.del(GlobalConstant.PARAM_LOGINID);
            Session.Clear();
            Response.Write("<script>window.top.location.replace('" + Url.Action("Index", "Login") + "')</script>");
            Response.Flush();
            Response.End();
        }

        public string SetYear(string year)
        {
            OperateSession.add(GlobalConstant.PARAM_SYSYEAR, year);
            return "ok";

        }


        /// <summary>
        /// 退出
        /// </summary>
        public void LogExit()
        {

            OperateSession.del(GlobalConstant.PARAM_USERNAME);
            OperateSession.del(GlobalConstant.PARAM_LOGINID);
            Session.Clear();
            Response.Write("<script>window.top.close()</script>");
            Response.Flush();
            Response.End();
        }

        public ActionResult Content()
        {

            return View();

        }

        public ActionResult ContentShow()
        {
            BLL.PrvFunctList prvFunctListBLL = new BLL.PrvFunctList();
            DataSet ds = new DataSet();
            if (OperateSession.get(GlobalConstant.PARAM_LOGINID) != "admin")
            {
                ds = prvFunctListBLL.getFunctList(OperateSession.get(GlobalConstant.PARAM_LOGINID), GlobalConstant.ENTITYNO);
            }
            else
            {
                ds = prvFunctListBLL.GetList(" fvisible='Y'");
            }

            DataTable dt = ds.Tables[0];

            string str = "";

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (str == "")
                {
                    str += dt.Rows[i]["FFunctNo"];
                }
                else
                {
                    str += "," + dt.Rows[i]["FFunctNo"];
                }
            }

            ViewData["NoList"] = str;

            return View();

        }

        public string GetUrl(string FFunctNo)
        {
            BLL.PrvFunctList prvFunctListBLL = new BLL.PrvFunctList();
            Model.PrvFunctList pModel = prvFunctListBLL.GetModelByFunctNo(FFunctNo);
            return  pModel.FfunctURL;
        }

        public ActionResult Left(string focusNodeName = "")
        {
            Models.TreeModelList treeModelList = new Models.TreeModelList();

            BLL.PrvFunctList prvFunctListBLL = new BLL.PrvFunctList();
            DataSet ds = new DataSet();
            if (OperateSession.get(GlobalConstant.PARAM_LOGINID) != "admin")
            {
                 ds = prvFunctListBLL.getFunctList(OperateSession.get(GlobalConstant.PARAM_LOGINID), GlobalConstant.ENTITYNO);
            }
            else
            {
                ds = prvFunctListBLL.GetList(" fvisible='Y'");
            }
            if (ds != null)
            {
                DataRow[] drmain = ds.Tables[0].Select("FFunctNo='m_EnterpriseFund'");
              
                if (drmain.Count() > 0)
                {
                    //TreeItemModel treeParentItemModel = new TreeItemModel(null)
                    //{
                    //    Id = "Main",
                    //    Name = drmain[0]["FfunctName"].ToString(),
                    //    ImageUrl = Url.Content(drmain[0]["FPictName"] == null ? "" : drmain[0]["FPictName"].ToString())
                    //};
                    //if (drmain[0]["FPictName"] != null && drmain[0]["FPictName"].ToString() != "")
                    //{
                    //    treeParentItemModel.ImageUrl = Url.Content(drmain[0]["FPictName"].ToString());
                    //}
                    DataRow[] drson = ds.Tables[0].Select("FPreFunctNo='" + drmain[0]["FFunctNo"].ToString() + "'");

                    foreach (DataRow dr in drson)
                    {
                        TreeItemModel treesonItemModel = new TreeItemModel(null)
                        {
                            Id = dr["FfunctURL"].ToString() + "|" + dr["FFunctNo"].ToString(),
                            Name = dr["FfunctName"].ToString(),
                            //ImageUrl = Url.Content(dr["FPictName"].ToString())
                        };
                        if (dr["FPictName"] != null && dr["FPictName"].ToString() != "")
                        {
                            treesonItemModel.ImageUrl = Url.Content(dr["FPictName"].ToString());
                        }
                        GetChildFuncList(dr, ds, treesonItemModel);

                        treeModelList.Add(treesonItemModel);
                    }
                }
            }
            return View(treeModelList);
        }

        
        /// <summary>
        /// 递归获取树形菜单
        /// </summary>
        /// <param name="prvChildList">功能菜单列表</param>
        /// <param name="treeParentItemModel">父级树节点</param>
        /// <param name="prvFunctListList">功能菜单集合</param>
        private void GetChildFuncList(DataRow dr, DataSet ds, TreeItemModel preTreeItemModel)
        {

            DataRow[] drson = ds.Tables[0].Select("FPreFunctNo='" + dr["FFunctNo"].ToString() + "'");

            foreach (DataRow drz in drson)
            {
                TreeItemModel treesonItemModel = new TreeItemModel(preTreeItemModel)
                {
                    Id = drz["FfunctURL"].ToString() + "|" + drz["FFunctNo"].ToString(),
                    Name = drz["FfunctName"].ToString(),
                    //ImageUrl = Url.Content(dr["FPictName"].ToString())
                };
                if (drz["FPictName"] != null && drz["FPictName"].ToString() != "")
                {
                    treesonItemModel.ImageUrl = Url.Content(drz["FPictName"].ToString());
                }
                GetChildFuncList(drz, ds, treesonItemModel);
            }

        }
    }
}
